/*
 * Copyright (C) 2014 luke.
 * 
 * This file is part of Silvie.
 * 
 * Silvie is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Silvie is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Silvie.  If not, see <http://www.gnu.org/licenses/>.
 */

package silvie.resources.images;

import java.io.File;

import silvie.Silvie;
import silvie.gui.Preview;
import silvie.gui.previewers.AbstractPreviewer;
import silvie.gui.previewers.ImagePreviewer;
import silvie.resources.AbstractResource;
import utils.DataReader;
import utils.PalettizedImage;

/**
 * Abstract class containing all attributes and methods that are shared across
 * image types (PAK and RAW).
 * 
 * <p>
 * This class extends AbstractResource because the Previewer is the same for all
 * image types.
 * 
 * @author luke
 */
public abstract class AbstractImage extends AbstractResource {
	protected PalettizedImage palettizedImage;
	protected int widthOffset;
	protected int heightOffset;
	protected int paletteOffset;
	protected int pixelIndicesOffset;

	/**
	 * AbstractImage constructor.
	 * 
	 * @param image the file containing the image.
	 */
	public AbstractImage(File image) {
		super(image, false);
		this.palettizedImage = new PalettizedImage();
		this.widthOffset = 0;
		this.heightOffset = 0;
		this.paletteOffset = 0;
		this.pixelIndicesOffset = 0;
	}

	/**
	 * Reads the dimensions of the image at offsets {@code widthOffset} and
	 * {@code heightOffset}.
	 */
	public void readDimensions() {
		byte[] imageBytes = getImageBytes();
		int width = DataReader.readInt(imageBytes, this.widthOffset);
		int height = DataReader.readInt(imageBytes, this.heightOffset);
		this.palettizedImage.setWidth(width);
		this.palettizedImage.setHeight(height);
	}

	/**
	 * Reads the 768-byte color palette at offset {@code paletteOffset} and
	 * stores it in a byte array.
	 */
	public void readPalette() {
		byte[] palette = new byte[Silvie.PALETTE_SIZE];
		System.arraycopy(getImageBytes(), this.paletteOffset, palette, 0, Silvie.PALETTE_SIZE);
		this.palettizedImage.setPalette(palette);
	}

	/**
	 * Reads the pixel indices at offset {@code pixelIndicesOffset} and stores
	 * them in a short array.
	 */
	public void readPixelIndices() {
		int pixelCount = this.palettizedImage.getWidth() * this.palettizedImage.getHeight();
		short[] pixelIndices = DataReader.readIndices(getImageBytes(), this.pixelIndicesOffset, pixelCount);
		this.palettizedImage.setPixelIndices(pixelIndices);
	}

	@Override
	public AbstractPreviewer getPreviewer(Preview preview) {
		return new ImagePreviewer(preview, this.palettizedImage.createImage());
	}

	/**
	 * Returns the resource bytes. This method can be overridden in case the
	 * bytes need to be unpacked.
	 * 
	 * @return the (unpacked) resource bytes.
	 */
	protected byte[] getImageBytes() {
		return this.resourceBytes;
	}
}
